/**
 * 
 */
package com.cgbsoft.financial;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.cgbsoft.financial.common.ConfigurationManager;

/**
 * @author CX
 *
 */
public class Login extends Activity implements OnClickListener {
	
	public final static String TAG = "Login";
	
	private EditText password;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		
		password = (EditText) findViewById(R.id.password);
		
		findViewById(R.id.submit_button).setOnClickListener(this);
		findViewById(R.id.cancel_button).setOnClickListener(this);
		
		checkIfNeedPassword();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.submit_button: {
			checkPassword();
			break;
		}
		case R.id.cancel_button: {
			this.finish();
			break;
		}
		default:
			break;
		}
		
	}
	
	/**
	 * 检查密码，如果成功，跳转到主界面，如果不成功，提示重新输入
	 */
	private void checkPassword() {
		String inputPassword = password.getText().toString();
		String password = ConfigurationManager.getInstance(this).getStringConfig(ConfigurationManager.PASS_WORD, "");
		if (password.equals(inputPassword)) {
			goMainActivity();
		} else {
			Toast.makeText(Login.this, "密码不正确，请重新输入。", Toast.LENGTH_SHORT).show();
		}
	}
	
	/**
	 * 检查密码是否正确
	 */
	private void checkIfNeedPassword() {
		String isNeedPassword = ConfigurationManager.getInstance(this).getStringConfig(ConfigurationManager.NEED_PASSWORD, "");
		
		if (!"true".equalsIgnoreCase(isNeedPassword)) {
			goMainActivity();
		}
	}
	
	/**
	 * 跳转到主界面
	 */
	private void goMainActivity() {
		Intent intent = new Intent(Login.this, FinancialActivity.class);
		startActivity(intent);
		this.finish();
	}
}
